package com.view.mediator
{
	import com.ApplicationFacade;
	import org.puremvc.as3.multicore.interfaces.IMediator;
	import org.puremvc.as3.multicore.patterns.mediator.Mediator;
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.utilities.statemachine.StateMachine;
	import org.puremvc.as3.multicore.utilities.statemachine.State;
	
	import mx.controls.Alert;
	import mx.events.CloseEvent;

	public class ApplicationMediator extends Mediator implements IMediator
	{
		public static const NAME:String='ApplicationMediator';

		public function ApplicationMediator(viewComponent:Object)
		{
			super(NAME, viewComponent);
		}

		override public function onRegister():void
		{
			facade.registerMediator(new WelcomeViewMediator(app.welcomeView));
			facade.registerMediator(new MainViewMediator(app.mainView));
			facade.registerMediator(new MainViewValidator(app.mainView));
		}

		public function get app():PureFormSubmission
		{
			return viewComponent as PureFormSubmission;
		}

		override public function listNotificationInterests():Array
		{
			return [ApplicationFacade.SHOW_WELCOME, 
			ApplicationFacade.ENTER_COLLECTING, 
			ApplicationFacade.VALIDATED, 
			ApplicationFacade.SHOW_THANKS, 
			ApplicationFacade.SHOW_FAILED, 
			ApplicationFacade.LOGOUT, 
			ApplicationFacade.CONFIRM_LOGOUT, 
			StateMachine.CHANGED];
		}

		override public function handleNotification(note:INotification):void
		{
			switch (note.getName())
			{
				case ApplicationFacade.SHOW_WELCOME:
					app.appViewSelector=PureFormSubmission.WELCOMEVIEW;

					app.tracer.text="WELCOME note.getName: " + note.getName() + " note.getBody: " + note.getBody() as String;
					app.tracer.text+=" note.getType: " + note.getType() as String;
					break;
				case ApplicationFacade.ENTER_COLLECTING:
					sendNotification(ApplicationFacade.RESET_FORMS);
					app.appViewSelector=PureFormSubmission.MAINVIEW;

					app.tracer.text="COLLECTING note.getName: " + note.getName() + " note.getBody: " + note.getBody() as String;
					app.tracer.text+=" note.getType: " + note.getType() as String;
					break;
				case ApplicationFacade.VALIDATED:
					app.tracer.text="VALIDATED note.getName: " + note.getName() + " note.getBody: " + note.getBody() as String;
					app.tracer.text+=" note.getType: " + note.getType() as String;
					break;
				case ApplicationFacade.LOGOUT:
					app.appViewSelector=PureFormSubmission.WELCOMEVIEW;

					app.tracer.text="LOGOUT note.getName: " + note.getName() + " note.getBody: " + note.getBody() as String;
					app.tracer.text+=" note.getType: " + note.getType() as String;
					break;
				case ApplicationFacade.SHOW_THANKS:
					app.tracer.text="SHOW_THANKS note.getName: " + note.getName() + " note.getBody: " + note.getBody() as String;
					app.tracer.text+=" note.getType: " + note.getType() as String;
					break;
				case ApplicationFacade.SHOW_FAILED:
					app.tracer.text="SHOW_FAILED note.getName: " + note.getName() + " note.getBody: " + note.getBody() as String;
					app.tracer.text+=" note.getType: " + note.getType() as String;
					break;
				case ApplicationFacade.CONFIRM_LOGOUT:
					var isWorkSaved:Boolean=note.getBody() as Boolean;
					if (!isWorkSaved && note.getType() == "state/welcoming")
					{
						sendNotification(StateMachine.CANCEL);
						Alert.show("Data not saved.\nExit anyway?", "Warning!", Alert.YES | Alert.NO, null, onAlertClose);
					}
					
					app.tracer.text="CONFIRM EXIT note.getName: " + note.getName() + " note.getBody: " + note.getBody() as String;
					app.tracer.text+=" note.getType: " + note.getType() as String;
					break;
				case StateMachine.CHANGED:
					app.tracer.text+="\n->->->STATE: " + State(note.getBody()).name + "<-<-<- ";
					app.tracer.text+="CHANGED: " + State(note.getBody()).changed + "; ";
					app.tracer.text+="EXITING: " + State(note.getBody()).exiting + "; ";
					app.tracer.text+="ENTERING: " + State(note.getBody()).entering + "; ";
					break;
			}
		}

		private function onAlertClose(event:CloseEvent):void
		{
			if (event.detail == 1)
			{
				sendNotification(StateMachine.ACTION, true, ApplicationFacade.LOGOUT);
			}
		}
	}
}